<template>
    <div class="home-singer">
        <h1>热门歌手</h1>
        <div class="wrapper" ref="wrapper">
            <!-- 滚动所需要的容器 -->
            <ul class="content" ref="content">
                <li v-for="(item,index) in listData" :key="index" @click="selectItem(item)">
                    <div class="cover">
                        <van-image :src="item.singer_pic" radius="1rem" />
                    </div>
                    <span>{{item.singer_name}}</span>
                </li>
            </ul>
        </div>

    </div>
</template>

<script>
    import { betterScrollHorizontal } from "../../common/betterScroll.js";
    export default {
        data() {
            return {
                listData: []
            }
        },
        /* 一个vue实例被生成后会调用created函数
        一般可以在此函数中调用ajax获取页面 */
        created() {
            this.getSingerListData()
        },
        methods: {
            //获取推荐歌单数据
            getSingerListData() {
                this.$request('get', '/singer/list').then(res => {
                    console.log(res)
                    // let arr = []
                    if (res.result == 100) {
                        this.listData = res.data.list.slice(0, 10);
                        // for (let index = 0; index < 10; index++) {
                        //     arr[index] = this.listData[index];
                        // }
                        // this.listData = arr
                    }

                    // 异步数据的一些处理
                    betterScrollHorizontal(
                        this,
                        this.$refs.wrapper,
                        this.$refs.content,
                        this.listData.length,
                        10
                    );
                });
            },
            selectItem(item) {
                const id = item.singer_mid;
                // console.log(id)
                //编程式导航，字符串路径的方式
                this.$store.dispatch('saveSinger', item)
                this.$router.push(`/singerHome/${id}`);
            }
        },

    };
</script>

<style scoped>
    .wrapper {
        width: 100%;
        overflow: hidden;
    }

    ul.content li {
        width: 10rem;
        display: inline-block;
        vertical-align: top;
    }

    ul.content li * {
        margin: 0 0.1rem;
    }

    ul.content li>span {
        font-size: 0.8rem;
        color: #625858;
    }
</style>